﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace KBS8.Shared
{
    public class Map
    {
        public List<MapTile> Tiles { get; set; }
        public int ColumnCount, RowCount, ZoomLevel;
        private int minMapSize;

        public Map()
        {
            Tiles = new List<MapTile>();
            ColumnCount = 0;
            RowCount = 0;
            ZoomLevel = 1;
            minMapSize = 2;
        }

        public void Fill(int rows, int columns)
        {
            Tiles.Clear();

            int i = 1;
            for (int y = 0; y < rows; y++)
            {
                RowCount = y + 1;
                for (int x = 0; x < columns; x++)
                {
                    MapTile tile = new MapTile(x, y, ZoomLevel, i, Colors.Black);
                    Tiles.Add(tile);
                    ColumnCount = x + 1;
                    i++;
                }
            }
        }

        public void InsertTilesTop(int rows)
        {
            for (int row = 0; row < rows; row++)
            {
                foreach (MapTile tile in Tiles)
                {
                    tile.Y++;
                }
                for (int x = 0; x < ColumnCount; x++)
                {
                    MapTile tile = new MapTile(x, 0, ZoomLevel, 100, Colors.Blue);
                    Tiles.Add(tile);
                }
                RowCount++;
            }
        }

        public void InsertTilesRight(int columns)
        {
            for (int column = 0; column < columns; column++)
            {
                for (int y = 0; y < RowCount; y++)
                {
                    MapTile tile = new MapTile(ColumnCount, y, ZoomLevel, 100, Colors.Blue);
                    Tiles.Add(tile);
                }
                ColumnCount++;
            }
        }

        public void InsertTilesBottom(int rows)
        {
            for (int row = 0; row < rows; row++)
            {
                for (int x = 0; x < ColumnCount; x++)
                {
                    MapTile tile = new MapTile(x, RowCount, ZoomLevel, 100, Colors.Blue);
                    Tiles.Add(tile);
                }
                RowCount++;
            }
        }

        public void InsertTilesLeft(int columns)
        {
            for (int column = 0; column < columns; column++)
            {
                foreach (MapTile tile in Tiles)
                {
                    tile.X++;
                }
                for (int y = 0; y < RowCount; y++)
                {
                    MapTile tile = new MapTile(0, y, ZoomLevel, 100, Colors.Blue);
                    Tiles.Add(tile);
                }
                ColumnCount++;
            }
        }

        public void DeleteTilesTop(int rows)
        {
            for (int row = 0; row < rows; row++)
            {
                if (RowCount > minMapSize)
                {
                    List<MapTile> tempTiles = new List<MapTile>();
                    foreach (MapTile tile in Tiles)
                    {
                        if (tile.Y == 0)
                            tempTiles.Add(tile);
                        tile.Y--;
                    }

                    foreach (MapTile tile in tempTiles)
                    {
                        Tiles.Remove(tile);
                    }
                    RowCount--;
                }
            }
        }

        public void DeleteTilesRight(int columns)
        {
            for (int column = 0; column < columns; column++)
            {
                if (ColumnCount > minMapSize)
                {
                    List<MapTile> tempTiles = new List<MapTile>();
                    foreach (MapTile tile in Tiles)
                    {
                        if (tile.X == ColumnCount - 1)
                            tempTiles.Add(tile);
                    }

                    foreach (MapTile tile in tempTiles)
                    {
                        Tiles.Remove(tile);
                    }
                    ColumnCount--;
                }
            }
        }

        public void DeleteTilesBottom(int rows)
        {
            for (int row = 0; row < rows; row++)
            {
                if (RowCount > minMapSize)
                {
                    List<MapTile> tempTiles = new List<MapTile>();
                    foreach (MapTile tile in Tiles)
                    {
                        if (tile.Y == RowCount - 1)
                            tempTiles.Add(tile);
                    }

                    foreach (MapTile tile in tempTiles)
                    {
                        Tiles.Remove(tile);
                    }
                    RowCount--;
                }
            }
        }

        public void DeleteTilesLeft(int columns)
        {
            for (int column = 0; column < columns; column++)
            {
                if (ColumnCount > minMapSize)
                {
                    List<MapTile> tempTiles = new List<MapTile>();
                    foreach (MapTile tile in Tiles)
                    {
                        if (tile.X == 0)
                            tempTiles.Add(tile);
                        tile.X--;
                    }

                    foreach (MapTile tile in tempTiles)
                    {
                        Tiles.Remove(tile);
                    }
                    ColumnCount--;
                }
            }
        }
    }
}
